package com.jk.interceptor;

import com.jk.controller.ClientLoginController;
import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/***
 * @ClassName: MyInterceptor
 * @Description:TODO html页面拦截器
 * @Auther: TaiBai
 * @Date: 2021/7/8 14:45
 * @version : V1.0
 */

public class MyInterceptor implements HandlerInterceptor {
    private static Logger logger = LoggerFactory.getLogger(ClientLoginController.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求的路径
        String url = request.getServletPath().toString();
        logger.info("url:" + url);
        /*//1、静态资源直接放行html img css js
        if (request.getServletPath().contains(".")) {
            return true;
        }
        //2、放行登录请求
        if (request.getServletPath().endsWith("login") || request.getServletPath().endsWith("clent")) {
            return true;
        }
        //3、下载pdf放行  .pdf下载
        if (request.getServletPath().endsWith("download")) {
            return true;
        }*/
        if (url.contains("clent")){
            return true;
        }
        //景点和游记详情拦截
        String aa = "nav/travelnotes/*.html";
        if (url.equals(aa) || url.contains("http://127.0.0.1:8085/nav/zoo/index.html") ){
            // 验证session是否存在
            Object obj = request.getSession().getAttribute("clent");
            if (obj == null) {
                response.sendRedirect("http://127.0.0.1:8085/clent/loginClient");
                return false;
            }
            return true;
        }

       /* //7非法请求 即这些请求需要登录后才能访问
        Response baseResponse = new Response();
        baseResponse.setCode("403");
        baseResponse.setMessage("权限不够");


        response.setContentType("text/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(baseResponse);*/
        return true;


    }

}
